package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbUserExample;
import com.pyg.pojo.TbUserExample.Criteria;
import com.pyg.mapper.TbUserMapper;
import com.pyg.pojo.TbUser;
import com.pyg.goods.service.TbUserService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbUserServiceImpl implements TbUserService {
	@Autowired
	private TbUserMapper tbUserMapper;

	@Override
	public List<TbUser> findAll() {
		return tbUserMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbUser> page = (Page<TbUser>) tbUserMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbUser
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbUser tbUser, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbUserExample example = null;
		if (tbUser != null) {
			example = new TbUserExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbUser.getUsername())) {
				criteria.andUsernameLike("%" + tbUser.getUsername() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getPassword())) {
				criteria.andPasswordLike("%" + tbUser.getPassword() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getPhone())) {
				criteria.andPhoneLike("%" + tbUser.getPhone() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getEmail())) {
				criteria.andEmailLike("%" + tbUser.getEmail() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getCreated())) {
				criteria.andCreatedEqualTo(tbUser.getCreated() );
			}
			if (PygStringUtils.isNotEmpty(tbUser.getUpdated())) {
				criteria.andUpdatedEqualTo(tbUser.getUpdated() );
			}
			if (PygStringUtils.isNotEmpty(tbUser.getSourceType())) {
				criteria.andSourceTypeLike("%" + tbUser.getSourceType() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getNickName())) {
				criteria.andNickNameLike("%" + tbUser.getNickName() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getName())) {
				criteria.andNameLike("%" + tbUser.getName() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getStatus())) {
				criteria.andStatusLike("%" + tbUser.getStatus() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getHeadPic())) {
				criteria.andHeadPicLike("%" + tbUser.getHeadPic() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getQq())) {
				criteria.andQqLike("%" + tbUser.getQq() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getAccountBalance())) {
				criteria.andAccountBalanceEqualTo(tbUser.getAccountBalance() );
			}
			if (PygStringUtils.isNotEmpty(tbUser.getIsMobileCheck())) {
				criteria.andIsMobileCheckLike("%" + tbUser.getIsMobileCheck() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getIsEmailCheck())) {
				criteria.andIsEmailCheckLike("%" + tbUser.getIsEmailCheck() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getSex())) {
				criteria.andSexLike("%" + tbUser.getSex() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbUser.getUserLevel())) {
				criteria.andUserLevelEqualTo(tbUser.getUserLevel() );
			}
			if (PygStringUtils.isNotEmpty(tbUser.getPoints())) {
				criteria.andPointsEqualTo(tbUser.getPoints() );
			}
			if (PygStringUtils.isNotEmpty(tbUser.getExperienceValue())) {
				criteria.andExperienceValueEqualTo(tbUser.getExperienceValue() );
			}
			if (PygStringUtils.isNotEmpty(tbUser.getBirthday())) {
				criteria.andBirthdayEqualTo(tbUser.getBirthday() );
			}
			if (PygStringUtils.isNotEmpty(tbUser.getLastLoginTime())) {
				criteria.andLastLoginTimeEqualTo(tbUser.getLastLoginTime() );
			}
		}

		Page<TbUser> page = (Page<TbUser>) tbUserMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbUser tbUser) {
		tbUserMapper.insert(tbUser);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbUserMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbUser findOne(Long id) {
		return tbUserMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbUser tbUser) {
		tbUserMapper.updateByPrimaryKey(tbUser);
	}

}
